define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'robot/index' + location.search,
                    add_url: 'robot/add',
                    edit_url: 'robot/edit',
                    del_url: 'robot/del',
                    multi_url: 'robot/multi',
                    import_url: 'robot/import',
                    table: 'robot',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'thumb',
                            title: __('Thumb'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.images,
                            events: Table.api.events.image,
                        },
                        {
                            field: 'ai_type',
                            title: '类型',
                            operate: 'LIKE',
                            table: table,
                            searchList: {
                                "1": 'ollama',
                                "2": 'aifox(chatgpt)',
                                "3": '数字先锋(sd绘图)',
                            },
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'model',
                            title: __('Model'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'prompt',
                            title: __('Prompt'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'creator',
                            title: __('Creator'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                        },
                        {
                            field: 'created_at',
                            title: __('Created_at'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'updater',
                            title: __('Updater'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                        },
                        {
                            field: 'updated_at',
                            title: __('Updated_at'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            Controller.modelListen();
        },
        edit: function () {
            Controller.api.bindevent();
            Controller.modelListen();
        },
        modelListen: function () {
            $(document).on('change', '#c-ai_type', function () {
                $('#c-model').selectPageClear();
            });
            $("#c-model").data("params", function (obj) {
                return {custom: {type: $("#c-ai_type option:selected").val()}};
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
